

/*Code for transmission of RTTY string borrowed from Robert Harrison. 
  http://www.robertharrison.org/icarus/wordpress/about/

  Transmitted string i  broken into each byte (character) with rtty_txstring and each byte
  into its corresponding bits with rtty_txbyte. The bits are then written out to Arduino pins
  with rtty_txbit.
*/

void rtty_txstring (char *string) // *variable is a pointer to the variable
{
        
	/* Simple function to send a char at a time to 
	** rtty_txbyte function. 
	** Each char is one byte (8 Bits)
	*/

	char c;

	c = *string++;
        
	while ( c != '\0') // \0 denotes end of string
	{
		rtty_txbyte (c);
		c = *string++;  //move c to next char of string
               
	}
}


void rtty_txbyte (char c)
{
	/* Simple function to sent each bit of a char to 
	** rtty_txbit function. 
	** The bits are sent Least Significant Bit first
	**
	** All chars should be preceded with a 0 and 
	** proceded with a 1. 0 = Start bit; 1 = Stop bit
	*/


	rtty_txbit (0); // Start bit

	// Send bits for char LSB first	

	for (int i=0;i<8;i++) //8 bits in char (byte).
	{
		if (c & 1) rtty_txbit(1); //If bit=1, 1 is sent to rtty_txbit function

			else rtty_txbit(0);	

		c = c >> 1; //c is shifted one to the right.

	}

	rtty_txbit (1); // Stop bit
}

void rtty_txbit (int bit)
{
        /* Function that sends the RTTY string out to the arduino pins.
	** rtty1 and rtty2 are the pins on the arduino connected to
	** the NTX2 module.
	*/

		if (bit)
		{
		  // 1
                    digitalWrite(rtty1,LOW);
                    digitalWrite(rtty2, HIGH);
                   
                    
		}
		else
		{
		  // 0
                    digitalWrite(rtty2, LOW);
                    digitalWrite(rtty1, HIGH);
                    
		}
                delay(19);
		delayMicroseconds(975); //Delay between transmissions to get the correct Baud rate.
                //delay(500);
                //analogWrite(3, 248);
                //delay(1000);
                
}






String format_time(char *string){
  
  char c;
  String time;
  for (int i = 0; i<strlen(string)-3;i++){
    c = string[i];
    if(i == 3 || i == 5){time += ':';}
    time += c;
  }
  
  return time;
}
